import Footer from '@/components/Footer';
import {register} from '@/services/ant-design-pro/api';
import {LockOutlined, UserOutlined} from '@ant-design/icons';
import {LoginForm, ProFormText} from '@ant-design/pro-components';
import {message, Tabs} from 'antd';
import React, {useState} from 'react';
import {history} from 'umi';
import styles from './index.less';


const Register: React.FC = () => {

  const [type, setType] = useState<string>('account');

  const handleSubmit = async (values: API.RegisterParams) => {
    //椒盐两次密码
    const {userPassword, checkPassword} = values;
    if (userPassword !== checkPassword) {
      message.error("两次输入密码不一致");
      return;
    }

    try {
      // 注册
      const id = await register(values);
      if (id) {
        const defaultLoginSuccessMessage = '注册成功！';
        message.success(defaultLoginSuccessMessage);
        /** 此方法会跳转到 redirect 参数所在的位置 */
        if (!history) return;
        //登录后重定向到之前的位置
        const {query} = history.location;
        history.push({
          pathname: "/user/login",
          query
        });

        return;
      }
    } catch (error: any) {
      const defaultLoginFailureMessage = "注册失败请重试";
      message.error(defaultLoginFailureMessage);
    }
  };

  return (
    <div className={styles.container}>
      <div className={styles.content}>
        <LoginForm
          submitter={{
            searchConfig: {
              submitText: "注册",
            }
          }}
          logo={<img alt="logo" src="/snake.svg"/>}
          title="snake"
          subTitle={'用户中心'}
          onFinish={async (values) => {
            await handleSubmit(values as API.LoginParams);
          }}
        >
          <Tabs activeKey={type} onChange={setType}>
            <Tabs.TabPane key="account" tab={'注册账号'}/>
          </Tabs>


          {type === 'account' && (
            <>
              <ProFormText
                name="userAccount"
                fieldProps={{
                  size: 'large',
                  prefix: <UserOutlined className={styles.prefixIcon}/>,
                }}
                placeholder={'请输入账号'}
                rules={[
                  {
                    required: true,
                    message: '账号是必填项！',
                  },
                ]}
              />
              <ProFormText.Password
                name="userPassword"
                fieldProps={{
                  size: 'large',
                  prefix: <LockOutlined className={styles.prefixIcon}/>,
                }}
                placeholder={'密码'}
                rules={[
                  {
                    required: true,
                    message: '请输入密码！',
                  },
                  {
                    required: true,
                    type: 'string',
                    min: 8,
                    message: '长度不能小于8',
                  },
                ]}
              />
              <ProFormText.Password
                name="checkPassword"
                fieldProps={{
                  size: 'large',
                  prefix: <LockOutlined className={styles.prefixIcon}/>,
                }}
                placeholder={'确认密码'}
                rules={[
                  {
                    required: true,
                    message: '请确认密码！',
                  },
                  {
                    required: true,
                    type: 'string',
                    min: 8,
                    message: '长度不能小于8',
                  },
                ]}
              />
              <ProFormText
                name="planetCode"
                fieldProps={{
                  size: 'large',
                  prefix: <LockOutlined className={styles.prefixIcon}/>,
                }}
                placeholder={'输入星球编号'}
                rules={[
                  {
                    required: true,
                    message: '请输入星球编号！',
                  }
                ]}
              />
            </>
          )}

          <div
            style={{
              marginBottom: 24,
            }}
          >

          </div>
        </LoginForm>
      </div>
      <Footer/>
    </div>
  )
    ;
};
export default Register;
